package GenerateNewID;
import java.io.*;
import java.util.*;
public class reGenerator {
    public static String test_data ="src/data/2755223/test_data.txt";  // 输入数据
    public static String result_data ="src/data/2755223/ans.txt";      // 答案
    public static String test_data_newid ="src/data/2755223/test_data_newid.txt"; //替换id后的输入数据
    public static String result_data_newid ="src/data/2755223/ans_newid.txt";     //替换id后的答案
    public static int fold = 1;
    public ArrayList<DataCell> loadData(){
        String fileName= test_data;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(fileName));
        } catch (FileNotFoundException exception) {
            System.err.println(fileName + " File Not Found");
        }
        String line = "";
        ArrayList<DataCell> ret = new ArrayList<>();
        try {
            while ((line = reader.readLine()) != null) {
                ret.add(new DataCell(line));
            }
        } catch (IOException exception) {
            System.err.println(exception.getMessage());
        }
        return ret;
    }
    public ArrayList<String> loadResultData(){
        String fileName = result_data;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(fileName));
        } catch (FileNotFoundException exception) {
            System.err.println(fileName + " File Not Found");
        }
        String line = "";
        ArrayList<String> ret = new ArrayList<String>();
        try {
            while ((line = reader.readLine()) != null) {
                    ret.add(line.trim());
            }
        } catch (IOException exception) {
            System.err.println(exception.getMessage());
        }
        return ret;
    }
    public static void writeFile(String path,String content){
        FileWriter fileWritter = null;
        try {
            fileWritter = new FileWriter(path,false);
            fileWritter.write(content);
            fileWritter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        reGenerator dg = new reGenerator();
        ArrayList<DataCell>  initData = dg.loadData();
        ArrayList<String>    iniRet   = dg.loadResultData();
        ArrayList<DataCell>  generaeData = new ArrayList<>();
        ArrayList<String>    generaeRet= new ArrayList<>();
        TreeSet<Integer> vset = new TreeSet<>();
        // 生成1份test_data
        for(DataCell e: initData){
            vset.add( e.IDFrom);
            vset.add( e.IDTo);
        }
        HashMap<Integer,Integer> idMap = new HashMap<>();
        //映射节点
        int id = 1;
        for(Integer v:vset){
            idMap.put(v,id);
            id++;
        }

        // 生成1份test_data
        for(DataCell e: initData){
            Integer newIdFrom = idMap.get(e.IDFrom);
            Integer newIdTo =   idMap.get(e.IDTo);
            DataCell n = new DataCell(newIdFrom,newIdTo,e.amount);
            generaeData.add(n);
        }

        //生成答案

        // 生成1份result
        ArrayList<String> curRet = new ArrayList<>();
        iniRet.remove(0);
        for(String line:iniRet){
            StringBuilder a= new StringBuilder();
            String[] split = line.split(",");
            for(String s:split){
                a.append(idMap.get(Integer.valueOf(s))+",");
            }
            a.deleteCharAt(a.length()-1);
            curRet.add(a+"");
        }
        // 生成写入的字符串
        StringBuilder  test_write = new StringBuilder();
        for(DataCell e:generaeData){
            test_write.append(e.IDFrom+","+e.IDTo+","+e.amount+"\n");
        }
        FileWriter fileWritter = null;
        try {
            fileWritter = new FileWriter(result_data_newid,false);
            fileWritter.write(curRet.size()+"\n");
            for(String e:curRet){
                fileWritter.write(e+"\n");
            }
            fileWritter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        reGenerator.writeFile(test_data_newid,test_write.toString());
    }
}

 class DataCell{
    public Integer IDFrom =0;
    public Integer IDTo =0;
    public Integer amount = 0;
    public DataCell(String line) {
        String[] row = line.trim().split(",");
        try {
            this.IDFrom = Integer.valueOf(row[0]);
            this.IDTo = Integer.valueOf(row[1]);
            this.amount =Integer.valueOf(row[2]);
        }catch (Exception e){
            int a=1;
        }

    }
    public DataCell(Integer IDFrom,Integer IDTo,Integer amount) {
        this.IDFrom = IDFrom;
        this.IDTo =   IDTo;
        this.amount = amount;
    }
}